<!-- build time:Tue Jul 21 2020 12:41:23 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/blog_favicon/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/blog_favicon/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="baidu-site-verification" content="r15J5zOS2KI43yBS"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:new URL("http://www.zhengjiaao.cn").hostname,root:"/",scheme:"Muse",version:"7.6.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:null},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta property="og:type" content="article"><meta property="og:title" content="Windows-下-Zookeeper-单机模式和伪分布式模式安装"><meta property="og:url" content="http://www.zhengjiaao.cn/posts/515071078.html"><meta property="og:site_name" content="宇宙小神特别萌"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-4054a58feea318c9.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-f7c9831861de0110.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-4be4d4ce19540407.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-8bc83c8c1fa29ded.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-5fb50db5c465ca54.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-f5a529ecc0c1d024.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-b07def4d0efc2a83.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-ba4036946f310d43.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-74f7df6296086357.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-2acd3cc68f8b2bce.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-4d50dc7c87992284.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-022af3181668b5e8.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-bdc3f3b553199601.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-39dd25e0bd9a2ae9.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-ecb0ad31f03bfe9b.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-fb4e84d2b613bbb2.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-bcc4c11a98a043e5.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-4b4757f23888938a.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-78b56866a118c4d1.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-a2caa102755b164c.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-2f22ce963b250b12.png"><meta property="article:published_time" content="2020-01-03T08:14:51.000Z"><meta property="article:modified_time" content="2020-07-20T10:37:55.329Z"><meta property="article:author" content="宇宙小神特别萌"><meta property="article:tag" content="Zookeeper"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/15645795-4054a58feea318c9.png"><link rel="canonical" href="http://www.zhengjiaao.cn/posts/515071078.html"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0}</script><title>Windows-下-Zookeeper-单机模式和伪分布式模式安装 | 宇宙小神特别萌</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript><link rel="alternate" href="/atom.xml" title="宇宙小神特别萌" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta custom-logo"><div class="site-meta-headline"><a><img class="custom-logo-image" src="/img/blog_favicon/favicon-32x32-next.png" alt="宇宙小神特别萌"></a></div><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">宇宙小神特别萌</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle">ZhengJa</p></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">27</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">116</span></a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a></li><li class="menu-item menu-item-messageboard"><a href="/messageboard/" rel="section"><i class="fa fa-fw fa-address-book"></i>留言板</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-fw fa-user-plus"></i>友链</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-fw fa-file-text-o"></i>说说</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a> <a href="https://github.com/zhengjiaao" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://www.zhengjiaao.cn/posts/515071078.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/my-images/avatar.gif"><meta itemprop="name" content="宇宙小神特别萌"><meta itemprop="description" content="人生就是一次幸运的旅行！"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="宇宙小神特别萌"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Windows-下-Zookeeper-单机模式和伪分布式模式安装</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-01-03 16:14:51" itemprop="dateCreated datePublished" datetime="2020-01-03T16:14:51+08:00">2020-01-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-07-20 18:37:55" itemprop="dateModified" datetime="2020-07-20T18:37:55+08:00">2020-07-20</time> </span><span id="/posts/515071078.html" class="post-meta-item leancloud_visitors" data-flag-title="Windows-下-Zookeeper-单机模式和伪分布式模式安装" title="热度"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">Valine：</span> <a title="valine" href="/posts/515071078.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/515071078.html" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>34k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>31 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><meta name="referrer" content="no-referrer"><a id="more"></a><p><strong>目录</strong><br><img src="https://upload-images.jianshu.io/upload_images/15645795-4054a58feea318c9.png" alt> <img src="https://upload-images.jianshu.io/upload_images/15645795-f7c9831861de0110.png" alt="zk目录.png"></p><p>Windows安装zk版本为：zookeeper-3.4.14</p><ul><li>1、单节点方式：部署在一台服务器上</li><li>2、单IP多节点（伪集群）：部署在同一IP ，但是有多个节点 ，各有自己的端口</li><li>3、多IP多节点：部署在不同IP ，各有自己的端口（未测试）</li></ul><h3 id="一、Zookeeper安装-单机部署"><a href="#一、Zookeeper安装-单机部署" class="headerlink" title="一、Zookeeper安装(单机部署)"></a>一、Zookeeper安装(单机部署)</h3><ul><li>单节点方式：部署在一台服务器上</li></ul><h4 id="1、下载地址："><a href="#1、下载地址：" class="headerlink" title="1、下载地址："></a>1、下载地址：</h4><p>链接：<a href="https://pan.baidu.com/s/1wwlsPdgTpJehC0bLCC4XAQ" target="_blank" rel="noopener">zookeeper-3.4.14.tar.gz</a> 提取码：l77v<br>下载完–解压缩后的目录：D:\zookeeper-3.4.14<br><img src="https://upload-images.jianshu.io/upload_images/15645795-4be4d4ce19540407.png" alt="zk解压缩后目录.png"></p><h4 id="2、添加tmp文件夹"><a href="#2、添加tmp文件夹" class="headerlink" title="2、添加tmp文件夹"></a>2、添加tmp文件夹</h4><p>添加tmp文件夹-用作存日志和数据 ，在目录D:\zookeeper-3.4.14\下<br><img src="https://upload-images.jianshu.io/upload_images/15645795-8bc83c8c1fa29ded.png" alt="tmp.png"></p><p>并且在tmp下添加data和log文件夹<br><img src="https://upload-images.jianshu.io/upload_images/15645795-5fb50db5c465ca54.png" alt="tmp2.png"></p><h4 id="3、打开D-zookeeper-3-4-14-conf"><a href="#3、打开D-zookeeper-3-4-14-conf" class="headerlink" title="3、打开D:\zookeeper-3.4.14\conf"></a>3、打开D:\zookeeper-3.4.14\conf</h4><p>把这个zoo_sample.cfg文件重命名为zoo.cfg<br><img src="https://upload-images.jianshu.io/upload_images/15645795-f5a529ecc0c1d024.png" alt="zoocfg.png"></p><p>打开zoo.cfg : 注意最好不要把中文注释复制进去 ，容易出现闪退情况 ，下面有出现闪退的解决方式。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">tickTime=<span class="number">2000</span></span><br><span class="line">initLimit=<span class="number">10</span></span><br><span class="line">syncLimit=<span class="number">5</span></span><br><span class="line"><span class="comment"># 配置数据存放地址 ，刚刚上面创建的文件夹</span></span><br><span class="line">dataDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\data  </span><br><span class="line"><span class="comment"># 配置日志存放地址 ，刚刚上面创建的文件夹</span></span><br><span class="line">dataLogDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\log   </span><br><span class="line"><span class="comment"># 默认断开2181  ，这里更改端口为2182</span></span><br><span class="line">clientPort=<span class="number">2182</span></span><br></pre></td></tr></table></figure><hr><blockquote><p><strong>ps:参数说明</strong></p><ul><li>tickTime：心跳间隔 ，这个时间作为zookeeper服务器之间或zookeeper服务器与客户端服务器维持心跳的时间间隔 ，即每隔 tickTime 时间就会发送一个心跳</li><li>initLimit：这个配置项是用来配置 Zookeeper 接受客户端（这里所说的客户端不是用户连接 Zookeeper 服务器的客户端 ，而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器）初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间（也就是 tickTime）长度后 Zookeeper 服务器还没有收到客户端的返回信息 ，那么表明这个客户端连接失败。总的时间长度就是 5<em>2000=10 秒</em></li><li><em>syncLimit：这个配置项表示 Leader 与 Follower 之间发送消息 ，请求和相应时间长度 ，最长不能超过多少个 tickTime 的时间长度 ，总的时间长度就是 2</em>2000=4 秒</li><li>dataDir：zookeeper存储数据的目录 ，默认情况下 ，zookeeper的日志问价也会保存至该目录</li><li>clientPort：客户端连接zookeeper的端口号</li><li>server.A=B：C：D：其中 A 是一个整形数字 ，表示服务器下标；B 是这个服务器的 ip 地址；C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口；D 表示的是万一集群中的 Leader 服务器挂了 ，需要一个端口来重新进行选举 ，选出一个新的 Leader ，而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式 ，由于 B 都是一样 ，所以不同的 Zookeeper 实例通信端口号不能一样 ，所以要给它们分配不同的端口号。</li></ul><hr></blockquote><h4 id="4、启动zookeeper"><a href="#4、启动zookeeper" class="headerlink" title="4、启动zookeeper"></a>4、启动zookeeper</h4><p>进入D:\zookeeper-3.4.14\bin<br><img src="https://upload-images.jianshu.io/upload_images/15645795-b07def4d0efc2a83.png" alt="zk启动.png"></p><p>双击zkServer.cmd ，也可以右击zkServer.cmd-发送到-桌面快捷方式 ，以后可以在桌面启动<br><img src="https://upload-images.jianshu.io/upload_images/15645795-ba4036946f310d43.png" alt="zk启动成功.png"></p><p>启动成功标识：binding to port 0.0.0.0/0.0.0.0:2182</p><p>cmd命令窗口闪退 ，这时可以修改zkServer.cmd文件内容 ，在末尾加上 pause关键字 ，之后再启动cmd窗口不会闪退 ，且可看到启动时所抛出的异常 ，可以根据异常描述进行相关处理 ，我的异常-注解中文乱码</p><h4 id="5、测试zookeeper是否可用："><a href="#5、测试zookeeper是否可用：" class="headerlink" title="5、测试zookeeper是否可用："></a>5、测试zookeeper是否可用：</h4><p>5.1 maven项目的pom.xml中先添加以下依赖项</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--zookeeper--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.9<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>5.2 最基本的示例程序</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.zk;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.*;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.data.Stat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/21 15:21</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZooKeeperHello</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, InterruptedException, KeeperException </span>&#123;</span><br><span class="line">        &lt;!--更改成本机ip地址测试 ，进入cmd 命令 ipconfig 查看IP--&gt;</span><br><span class="line">        ZooKeeper zk = <span class="keyword">new</span> ZooKeeper(<span class="string">"192.168.2.113:2182"</span>, <span class="number">300000</span>, <span class="keyword">new</span> DemoWatcher());<span class="comment">//连接zk server</span></span><br><span class="line">        String node = <span class="string">"/app1"</span>;</span><br><span class="line">        Stat stat = zk.exists(node, <span class="keyword">false</span>);<span class="comment">//检测/app1是否存在</span></span><br><span class="line">        <span class="keyword">if</span> (stat == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">//创建节点</span></span><br><span class="line">            String createResult = zk.create(node, <span class="string">"test"</span>.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);</span><br><span class="line">            System.out.println(createResult);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//获取节点的值</span></span><br><span class="line">        <span class="keyword">byte</span>[] b = zk.getData(node, <span class="keyword">false</span>, stat);</span><br><span class="line">        System.out.println(<span class="keyword">new</span> String(b));</span><br><span class="line">        zk.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoWatcher</span> <span class="keyword">implements</span> <span class="title">Watcher</span> </span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent event)</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"-----------&gt;"</span>);</span><br><span class="line">            System.out.println(<span class="string">"path:"</span> + event.getPath());</span><br><span class="line">            System.out.println(<span class="string">"type:"</span> + event.getType());</span><br><span class="line">            System.out.println(<span class="string">"stat:"</span> + event.getState());</span><br><span class="line">            System.out.println(<span class="string">"&lt;-----------"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>控制台打印效果：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">-----------&gt;</span><br><span class="line">path:null</span><br><span class="line">type:None</span><br><span class="line">stat:SyncConnected</span><br><span class="line">&lt;-----------</span><br><span class="line">15:24:45.503 [main-SendThread(192.168.2.113:2182)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x100015c2c980002, packet:: clientPath:null serverPath:null finished:false header:: 1,3  replyHeader:: 1,21,0  request:: &#39;&#x2F;app1,T  response:: s&#123;17,17,1566372182125,1566372182125,0,0,0,0,4,0,17&#125; </span><br><span class="line">15:24:45.510 [main-SendThread(192.168.2.113:2182)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x100015c2c980002, packet:: clientPath:null serverPath:null finished:false header:: 2,4  replyHeader:: 2,21,0  request:: &#39;&#x2F;app1,F  response:: #74657374,s&#123;17,17,1566372182125,1566372182125,0,0,0,0,4,0,17&#125; </span><br><span class="line">test</span><br></pre></td></tr></table></figure><p>单机部署完成 ，测试成功！<br>这个版本的zk默是支持远程连接的 ，不需要配置本机ip地址 ，也能让别人连接！</p><h3 id="二、Zookeeper-伪分布式安装（集群）"><a href="#二、Zookeeper-伪分布式安装（集群）" class="headerlink" title="二、Zookeeper 伪分布式安装（集群）"></a>二、Zookeeper 伪分布式安装（集群）</h3><ul><li><p>单IP多节点（伪集群）：部署在同一IP ，但是有多个节点 ，各有自己的端口。</p></li><li><p>伪分布式安装就是在同一台pc上安装 ，安装时使用同一个zookeeper包 ，多个配置文件分别配置为不同的端口.由于机器数量有限 ，这里采用伪分布式配置模拟集群配置。</p></li></ul><p>zk 版本：Windows安装zk版本为：zookeeper-3.4.14<br>下载链接：<a href="https://pan.baidu.com/s/1wwlsPdgTpJehC0bLCC4XAQ" target="_blank" rel="noopener">zookeeper-3.4.14.tar.gz</a> 提取码：l77v</p><h4 id="1、解压缩后的目录"><a href="#1、解压缩后的目录" class="headerlink" title="1、解压缩后的目录"></a>1、解压缩后的目录</h4><p>下载完–解压缩后的目录：D:\zookeeper-3.4.14<br><img src="https://upload-images.jianshu.io/upload_images/15645795-74f7df6296086357.png" alt="zk解压缩后目录.png"></p><h4 id="2、修改配置"><a href="#2、修改配置" class="headerlink" title="2、修改配置"></a>2、修改配置</h4><p>进入conf目录下把zoo_sample.cfg文件重名为：zoo.cfg ，并修改配置为如下:注意最好不要把中文注释复制进去 ，容易出现闪退情况</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">tickTime&#x3D;2000</span><br><span class="line">initLimit&#x3D;10</span><br><span class="line">syncLimit&#x3D;5</span><br><span class="line"># 配置数据存放地址 ，刚刚上面创建的文件夹</span><br><span class="line">dataDir&#x3D;D:\\zookeeper-3.4.14\\tmp\\data</span><br><span class="line"># 配置日志存放地址 ，刚刚上面创建的文件夹</span><br><span class="line">dataLogDir&#x3D;D:\\zookeeper-3.4.14\\tmp\\log </span><br><span class="line"># 默认断开2181  ，这里更改端口为2182</span><br><span class="line">clientPort&#x3D;2182</span><br></pre></td></tr></table></figure><h5 id="添加tmp文件夹"><a href="#添加tmp文件夹" class="headerlink" title="添加tmp文件夹"></a>添加tmp文件夹</h5><p>添加tmp文件夹 ，在目录D:\zookeeper-3.4.14\下:<br>D:\zookeeper-3.4.14\tmp<br><img src="https://upload-images.jianshu.io/upload_images/15645795-2acd3cc68f8b2bce.png" alt="tmp.png"></p><p>并且在tmp下添加data和log文件夹:D:\zookeeper-3.4.14\tmp\data、D:\zookeeper-3.4.14\tmp\log<br><img src="https://upload-images.jianshu.io/upload_images/15645795-4d50dc7c87992284.png" alt="tmp2.png"></p><h4 id="3、启动zookeeper服务："><a href="#3、启动zookeeper服务：" class="headerlink" title="3、启动zookeeper服务："></a>3、启动zookeeper服务：</h4><p>进入目录：D:\zookeeper-3.4.14\bin ，双击执行zkServer.cmd ，这样就启动了zookeeper服务了<br>关闭zkServer窗口 ，zookeeper服务器也就关闭了</p><h4 id="4、伪分布式安装"><a href="#4、伪分布式安装" class="headerlink" title="4、伪分布式安装"></a>4、伪分布式安装</h4><p>伪分布式安装就是在同一台pc上安装 ，安装时使用同一个zookeeper包 ，多个配置文件分别配置为不同的端口。我这里配置3个伪服务。</p><p>1.）<strong>将D:\zookeeper-3.4.14\conf\下的zoo.cfg分别复制出文件zoo1.cfg,zoo2.cfg,zoo3.cfg三个文件,并分别修改配置为：</strong><br><img src="https://upload-images.jianshu.io/upload_images/15645795-022af3181668b5e8.png" alt="zoo.png"></p><p><strong>分别修改配置为：</strong><br>zoo1.cfg 注意最好不要把中文注释复制进去 ，容易出现闪退情况</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">tickTime=<span class="number">2000</span></span><br><span class="line">initLimit=<span class="number">10</span></span><br><span class="line">syncLimit=<span class="number">5</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置数据存放地址 ，与下面创建的目录一致</span></span><br><span class="line">dataDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\data\\<span class="number">1</span>     </span><br><span class="line"><span class="comment">#配置日志存放地址 ，与下面创建的目录一致</span></span><br><span class="line">dataLogDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\log\\<span class="number">1</span>   </span><br><span class="line"><span class="comment"># 默认断开2181  ，这里更改端口为2182</span></span><br><span class="line">clientPort=<span class="number">2182</span>   </span><br><span class="line"></span><br><span class="line">server<span class="number">.1</span>=localhost:<span class="number">2887</span>:<span class="number">3887</span></span><br><span class="line">server<span class="number">.2</span>=localhost:<span class="number">2888</span>:<span class="number">3888</span></span><br><span class="line">server<span class="number">.3</span>=localhost:<span class="number">2889</span>:<span class="number">3889</span></span><br></pre></td></tr></table></figure><p>zoo2.cfg</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">tickTime=<span class="number">2000</span></span><br><span class="line">initLimit=<span class="number">10</span></span><br><span class="line">syncLimit=<span class="number">5</span></span><br><span class="line"></span><br><span class="line">dataDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\data\\<span class="number">2</span>   </span><br><span class="line">dataLogDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\log\\<span class="number">2</span></span><br><span class="line">clientPort=<span class="number">2182</span> </span><br><span class="line"></span><br><span class="line">server<span class="number">.1</span>=localhost:<span class="number">2887</span>:<span class="number">3887</span></span><br><span class="line">server<span class="number">.2</span>=localhost:<span class="number">2888</span>:<span class="number">3888</span></span><br><span class="line">server<span class="number">.3</span>=localhost:<span class="number">2889</span>:<span class="number">3889</span></span><br></pre></td></tr></table></figure><p>zoo3.cfg</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">tickTime=<span class="number">2000</span></span><br><span class="line">initLimit=<span class="number">10</span></span><br><span class="line">syncLimit=<span class="number">5</span></span><br><span class="line"></span><br><span class="line">dataDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\data\\<span class="number">3</span>    </span><br><span class="line">dataLogDir=D:\\zookeeper<span class="number">-3.4</span><span class="number">.14</span>\\tmp\\log\\<span class="number">3</span>   </span><br><span class="line">clientPort=<span class="number">2184</span>   </span><br><span class="line"></span><br><span class="line">server<span class="number">.1</span>=localhost:<span class="number">2887</span>:<span class="number">3887</span></span><br><span class="line">server<span class="number">.2</span>=localhost:<span class="number">2888</span>:<span class="number">3888</span></span><br><span class="line">server<span class="number">.3</span>=localhost:<span class="number">2889</span>:<span class="number">3889</span></span><br></pre></td></tr></table></figure><hr><blockquote><p>备注：<br>　　假设把配置文件表示为：zoo{num}.cfg, server.{num}=ip/domain:Port1:Port2<br>　　其中 num：表示数字表示第几号服务器；ip/domain ：是服务器域名或者ip地址。<br>　　 Port1：表示这个服务器和集群中的Leader服务器交换信息的端口；<br>　　 Port2：表示万一集群中的Leader服务器挂了 ，需要一个端口重新进行选举 ，选出一个新的Leader ，这个端口就是用来执行选举时服务器相互通信的端口。<br>​ 由于我们是伪集群 ，所以ip或者域名是一样的 ，所以要分配不同的端口号<br>​ server.A=B：C：D：其中 A 是一个整形数字 ，表示服务器下标,与myid文件中的id是一致的；B 是这个服务器的 ip 地址；C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口；D 表示的是万一集群中的 Leader 服务器挂了 ，需要一个端口来重新进行选举 ，选出一个新的 Leader ，而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式 ，由于 B 都是一样 ，所以不同的 Zookeeper 实例通信端口号不能一样 ，所以要给它们分配不同的端口号。</p></blockquote><hr><p><strong>创建目录</strong></p><p>创建存数据data子目录：<br>D:\zookeeper-3.4.14\tmp\data\1、D:\zookeeper-3.4.14\tmp\data\2、D:\zookeeper-3.4.14\tmp\data\3<br><img src="https://upload-images.jianshu.io/upload_images/15645795-bdc3f3b553199601.png" alt="伪集群data目录.png"></p><p>分别在三个文件下创建myid文件 ，文件内容依次为：1 ，2 ，3<br><img src="https://upload-images.jianshu.io/upload_images/15645795-39dd25e0bd9a2ae9.png" alt="myid.png"></p><p>创建存日志log子目录：<br>D:\zookeeper-3.4.14\tmp\log\1 ，D:\zookeeper-3.4.14\tmp\log\2、D:\zookeeper-3.4.14\tmp\log\3<br><img src="https://upload-images.jianshu.io/upload_images/15645795-ecb0ad31f03bfe9b.png" alt="伪集群log目录.png"></p><h4 id="5、修改zkServer-cmd"><a href="#5、修改zkServer-cmd" class="headerlink" title="5、修改zkServer.cmd"></a>5、修改zkServer.cmd</h4><p>进入D:\zookeeper-3.4.14\bin下复制文件zkServer.cmd为zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd<br><img src="https://upload-images.jianshu.io/upload_images/15645795-fb4e84d2b613bbb2.png" alt="zkServer修改.png"></p><p>5.1 修改zkServer-1.cmd 内容修改为如下：</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setlocal</span></span><br><span class="line"><span class="keyword">call</span> "%~dp0zkEnv.<span class="built_in">cmd</span>"</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain</span><br><span class="line"><span class="built_in">set</span> ZOOCFG=D:\\zookeeper-<span class="number">3</span>.<span class="number">4</span>.<span class="number">14</span>\\conf\\zoo3.cfg</span><br><span class="line"><span class="built_in">echo</span> on</span><br><span class="line"><span class="keyword">call</span> <span class="variable">%JAVA%</span> "-Dzookeeper.log.<span class="built_in">dir</span>=<span class="variable">%ZOO_LOG_DIR%</span>" "-Dzookeeper.root.logger=<span class="variable">%ZOO_LOG4J_PROP%</span>" -cp "<span class="variable">%CLASSPATH%</span>" <span class="variable">%ZOOMAIN%</span> "<span class="variable">%ZOOCFG%</span>" %*</span><br><span class="line"></span><br><span class="line"><span class="built_in">endlocal</span></span><br></pre></td></tr></table></figure><p>5.2 修改zkServer-2.cmd 内容修改为如下：</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setlocal</span></span><br><span class="line"><span class="keyword">call</span> "%~dp0zkEnv.<span class="built_in">cmd</span>"</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain</span><br><span class="line"><span class="comment">REM 添加配置路径 ZOOCFG</span></span><br><span class="line"><span class="built_in">set</span> ZOOCFG=D:\\zookeeper-<span class="number">3</span>.<span class="number">4</span>.<span class="number">14</span>\\conf\\zoo3.cfg</span><br><span class="line"><span class="built_in">echo</span> on</span><br><span class="line"><span class="keyword">call</span> <span class="variable">%JAVA%</span> "-Dzookeeper.log.<span class="built_in">dir</span>=<span class="variable">%ZOO_LOG_DIR%</span>" "-Dzookeeper.root.logger=<span class="variable">%ZOO_LOG4J_PROP%</span>" -cp "<span class="variable">%CLASSPATH%</span>" <span class="variable">%ZOOMAIN%</span> "<span class="variable">%ZOOCFG%</span>" %*</span><br><span class="line"></span><br><span class="line"><span class="built_in">endlocal</span></span><br></pre></td></tr></table></figure><p>5.3 修改zkServer-3.cmd 内容修改为如下：</p><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setlocal</span></span><br><span class="line"><span class="keyword">call</span> "%~dp0zkEnv.<span class="built_in">cmd</span>"</span><br><span class="line"></span><br><span class="line"><span class="built_in">set</span> ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain</span><br><span class="line"><span class="built_in">set</span> ZOOCFG=D:\\zookeeper-<span class="number">3</span>.<span class="number">4</span>.<span class="number">14</span>\\conf\\zoo3.cfg</span><br><span class="line"><span class="built_in">echo</span> on</span><br><span class="line"><span class="keyword">call</span> <span class="variable">%JAVA%</span> "-Dzookeeper.log.<span class="built_in">dir</span>=<span class="variable">%ZOO_LOG_DIR%</span>" "-Dzookeeper.root.logger=<span class="variable">%ZOO_LOG4J_PROP%</span>" -cp "<span class="variable">%CLASSPATH%</span>" <span class="variable">%ZOOMAIN%</span> "<span class="variable">%ZOOCFG%</span>" %*</span><br><span class="line"></span><br><span class="line"><span class="built_in">endlocal</span></span><br></pre></td></tr></table></figure><h4 id="6、配置完成-启动zkServer"><a href="#6、配置完成-启动zkServer" class="headerlink" title="6、配置完成 启动zkServer"></a>6、配置完成 启动zkServer</h4><p>分别启动zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd</p><p>进入目录：D:\zookeeper-3.4.14\bin ，分布执行zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd,启动伪分布式zookeeper集群 ，启动过程中如果前两个启动的服务户出现异常情况为正常 ，直到3个zkServer-x.cmd都启动完后就不会出现异常情况。</p><p>进入cmd执行命令：netstat -ano # 查看端口情况<br><strong>到此zk的伪分布式集群配置完毕！</strong></p><blockquote><p>注意：</p><p>同一IP上搭建多个节点的集群时 ，必须要注意端口问题 ，端口必须不一致才行；</p><p>创建多个节点集群时 ，在dataDir目录下必须创建myid文件 ，myid文件用于zookeeper验证server序号等 ，myid文件只有一行 ，并且为当前server的序号 ，例如server.1的myid就是1 ，server2的myid就是2等。</p><p>server.A=B:C:D;其中 A 是一个数字 ，表示这个是第几号服务器；B 是这个服务器的 ip 地址；C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口；D 表示的是万一集群中的 Leader 服务器挂了 ，需要一个端口来重新进行选举 ，选出一个新的 Leader ，而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式 ，由于 B 都是一样 ，所以不同的 Zookeeper 实例通信端口号不能一样 ，所以要给它们分配不同的端口号。</p></blockquote><hr><h4 id="7、配置中出现的问题及解决方式："><a href="#7、配置中出现的问题及解决方式：" class="headerlink" title="7、配置中出现的问题及解决方式："></a>7、配置中出现的问题及解决方式：</h4><blockquote><ul><li><p>1.启动zkServer.cmd 时 cmd命令窗口闪退</p><p>这时可以修改zkServer.cmd文件内容 ，在末尾加上 pause关键字 ，之后再启动cmd窗口不会闪退 ，且可看到启动时所抛出的异常 ，可以根据异常 描述进行相关处理 ，我的异常-注解中文乱码</p></li><li><p>2.有myid还报错Caused by: java.lang.IllegalArgumentException: myid file is missing</p><p>解决方式：将myid.txt 后缀去掉.txt ，只保留myid文件名</p></li><li><p>以上两个异常只是本人在安装过程中遇到的问题 ，可能还有其他其他异常我没遇到 ，下面是别人记录的异常</p></li><li><p>3.Error: JAVA_HOME is incorrectly set：缺少jdk或者jdk版本不匹配等相关问题</p></li><li><p>4.java.net.bindexception address already:端口号被占用</p></li><li><p>5.java.lang.numberformatexception:数字转换异常 ，出现原因可能是myid文件内容问题 ，或者在使用命令启动时在zkServer.cmd 多了其他内容 ，使用命令启动时如上图所示选择到zkServer.cmd即可 ，后面不需要任何内容</p></li></ul></blockquote><hr><h3 id="三、Zookeeper真分布式集群（未测试）"><a href="#三、Zookeeper真分布式集群（未测试）" class="headerlink" title="三、Zookeeper真分布式集群（未测试）"></a>三、Zookeeper真分布式集群（未测试）</h3><p>多IP多节点：部署在不同IP ，各有自己的端口（未测试）<br>多IP多节点：将zookeeper拷贝到每个节点一份。<br>多IP多节点与单IP多节点搭建过程基本一致 ，上述过程不再重复描述 ，仅重点说一个地方：server的IP地址、端口为真实即可。</p><p><strong>注意：zk的部署个数最好为基数 ，ZK集群的机制是只要超过半数的节点OK ，集群就能正常提供服务。只有ZK节点挂得太多 ，只剩一半或不到一半节点能工作 ，集群才失效。</strong></p><h5 id="1-Dubbo中zookeeper做注册中心-，如果注册中心集群都挂掉-，发布者和订阅者之间还能通信么？"><a href="#1-Dubbo中zookeeper做注册中心-，如果注册中心集群都挂掉-，发布者和订阅者之间还能通信么？" class="headerlink" title="1.Dubbo中zookeeper做注册中心 ，如果注册中心集群都挂掉 ，发布者和订阅者之间还能通信么？"></a>1.Dubbo中zookeeper做注册中心 ，如果注册中心集群都挂掉 ，发布者和订阅者之间还能通信么？</h5><p>启动dubbo时 ，消费者会从zk拉取注册的生产者的地址接口等数据 ，缓存在本地。每次调用时 ，按照本地存储的地址进行调用。但是在注册中心全部挂掉后增加新的提供者 ，则不能被消费者发现：</p><blockquote><p>健状性</p><ul><li>监控中心宕掉不影响使用 ，只是丢失部分采样数据</li><li>数据库宕掉后 ，注册中心仍能通过缓存提供服务列表查询 ，但不能注册新服务</li><li>注册中心对等集群 ，任意一台宕掉后 ，将自动切换到另一台</li><li>注册中心全部宕掉后 ，服务提供者和服务消费者仍能通过本地缓存通讯</li><li>服务提供者无状态 ，任意一台宕掉后 ，不影响使用</li><li>服务提供者全部宕掉后 ，服务消费者应用将无法使用 ，并无限次重连等待服务提供者恢复</li></ul></blockquote><p>Dubbo默认缓存的本地路径：C:\Users\Administrator\.dubbo<br>因此 ，就算zk服务关掉 ，也能继续访问项目。</p><h3 id="四、验证服务器"><a href="#四、验证服务器" class="headerlink" title="四、验证服务器"></a>四、验证服务器</h3><p>利用 <strong>zktools可视化</strong> 工具<br>工具下载地址：<a href="https://pan.baidu.com/s/17FY6ii6tQ_RMb7fzgHvG0g" target="_blank" rel="noopener">zktools可视化</a> 提取码：zv2f</p><p>使用方式：先启动zk ，再用 <strong>zktools可视化 *<em>连接指定的 *</em>ip:端口</strong><br><img src="https://upload-images.jianshu.io/upload_images/15645795-bcc4c11a98a043e5.png" alt="zktools工具使用.png"></p><p><strong>Dubbo中zookeeper做注册中心 ，如果注册中心集群都挂掉 ，发布者和订阅者之间还能通信么？</strong></p><p>启动dubbo时 ，消费者会从zk拉取注册的生产者的地址接口等数据 ，缓存在本地。每次调用时 ，按照本地存储的地址进行调用。但是在注册中心全部挂掉后增加新的提供者 ，则不能被消费者发现：</p><blockquote><p>健状性</p><ul><li>监控中心宕掉不影响使用 ，只是丢失部分采样数据</li><li>数据库宕掉后 ，注册中心仍能通过缓存提供服务列表查询 ，但不能注册新服务</li><li>注册中心对等集群 ，任意一台宕掉后 ，将自动切换到另一台</li><li>注册中心全部宕掉后 ，服务提供者和服务消费者仍能通过本地缓存通讯</li><li>服务提供者无状态 ，任意一台宕掉后 ，不影响使用</li><li>服务提供者全部宕掉后 ，服务消费者应用将无法使用 ，并无限次重连等待服务提供者恢复</li></ul></blockquote><h3 id="五、springboot-zookeeperk-dubbo"><a href="#五、springboot-zookeeperk-dubbo" class="headerlink" title="五、springboot zookeeperk dubbo"></a>五、springboot zookeeperk dubbo</h3><p>这里只介绍怎么配置 ，不过多详细介绍项目如何搭建及详细代码。下面调用的是<strong>二、Zookeeper 伪分布式安装（集群）</strong> 配置的端口：2183、2184、2185</p><h4 id="1、zk连接测试"><a href="#1、zk连接测试" class="headerlink" title="1、zk连接测试"></a>1、zk连接测试</h4><p>这里只是zk测试 ，不需要搭建dubbo+zk分部署项目。</p><h6 id="pom-xml依赖"><a href="#pom-xml依赖" class="headerlink" title="pom.xml依赖"></a>pom.xml依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><h6 id="测试集群是否搭建成功："><a href="#测试集群是否搭建成功：" class="headerlink" title="测试集群是否搭建成功："></a>测试集群是否搭建成功：</h6><p>测试类 ZooKeeperHello.java</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist.zk;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.I0Itec.zkclient.ZkClient;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.*;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.data.Stat;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/21 15:21</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZooKeeperHello</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, InterruptedException, KeeperException </span>&#123;</span><br><span class="line">        ZooKeeper zk = <span class="keyword">new</span> ZooKeeper(<span class="string">"192.168.2.113:2183"</span>, <span class="number">300000</span>, <span class="keyword">new</span> DemoWatcher());<span class="comment">//连接zk server</span></span><br><span class="line">        String node = <span class="string">"/app1"</span>;</span><br><span class="line">        Stat stat = zk.exists(node, <span class="keyword">false</span>);<span class="comment">//检测/app1是否存在</span></span><br><span class="line">        <span class="keyword">if</span> (stat == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">//创建节点</span></span><br><span class="line">            String createResult = zk.create(node, <span class="string">"test"</span>.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);</span><br><span class="line">            System.out.println(createResult);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//获取节点的值</span></span><br><span class="line">        <span class="keyword">byte</span>[] b = zk.getData(node, <span class="keyword">false</span>, stat);</span><br><span class="line">        System.out.println(<span class="keyword">new</span> String(b));</span><br><span class="line">        zk.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoWatcher</span> <span class="keyword">implements</span> <span class="title">Watcher</span> </span>&#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent event)</span> </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"-----------&gt;"</span>);</span><br><span class="line">            System.out.println(<span class="string">"path:"</span> + event.getPath());</span><br><span class="line">            System.out.println(<span class="string">"type:"</span> + event.getType());</span><br><span class="line">            System.out.println(<span class="string">"stat:"</span> + event.getState());</span><br><span class="line">            System.out.println(<span class="string">"&lt;-----------"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**Spring Boot2.0之 整合Zookeeper集群</span></span><br><span class="line"><span class="comment">     * 普通的连接：</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testZkClient</span><span class="params">()</span></span>&#123;</span><br><span class="line">        String connection = <span class="string">"192.168.2.113:2183,192.168.2.113:2184,192.168.2.113:2185"</span>;</span><br><span class="line">        ZkClient zkClient = <span class="keyword">new</span> ZkClient(connection);</span><br><span class="line">        zkClient.createPersistent(<span class="string">"/toov5_01"</span>);  <span class="comment">//添加节点</span></span><br><span class="line">        zkClient.close();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>执行测试后 ，使用zktools可视化工具查看是否成功。</p><p>工具下载地址：<a href="https://pan.baidu.com/s/17FY6ii6tQ_RMb7fzgHvG0g" target="_blank" rel="noopener">zktools可视化</a> 提取码：zv2f</p><p>使用方式：先启动zk ，再用 <strong>zktools可视化 *<em>连接指定的 *</em>ip:端口</strong></p><p><img src="https://upload-images.jianshu.io/upload_images/15645795-4b4757f23888938a.png" alt="zktools工具使用.png"></p><p>测试：zk端口2183：</p><p><img src="https://upload-images.jianshu.io/upload_images/15645795-78b56866a118c4d1.png" alt="zktools工具使用2.png"></p><p>zk端口2184：</p><p><img src="https://upload-images.jianshu.io/upload_images/15645795-a2caa102755b164c.png" alt="zktools工具使用3.png"></p><p>在zk端口2185也添加节点成功 ，说明集群搭建成功。</p><h4 id="2、单机测试-分布式-dubbo"><a href="#2、单机测试-分布式-dubbo" class="headerlink" title="2、单机测试-分布式(dubbo)"></a>2、单机测试-分布式(dubbo)</h4><p>需要搭建springboot+zookeeper+dubbo分布式测试项目</p><h5 id="web层-消费者-配置"><a href="#web层-消费者-配置" class="headerlink" title="web层(消费者)配置"></a>web层(消费者)配置</h5><h6 id="1）-web层-消费者-引入依赖pom-xml"><a href="#1）-web层-消费者-引入依赖pom-xml" class="headerlink" title="1）.web层(消费者)- 引入依赖pom.xml"></a>1）.web层(消费者)- 引入依赖pom.xml</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--dubbo依赖--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><h6 id="2）-web层-消费者-yml-文件"><a href="#2）-web层-消费者-yml-文件" class="headerlink" title="2）.web层(消费者)- *.yml 文件"></a>2）.web层(消费者)- *.yml 文件</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># dubbo配置</span><br><span class="line">dubbo:</span><br><span class="line">  application:</span><br><span class="line">    name: consumer # 消费方应用名 ，用于计算依赖关系 ，不要与提供方一样</span><br><span class="line">  registry:</span><br><span class="line">    protocol: zookeeper   #dubbo&#x2F;zookeeper 协议</span><br><span class="line">    #address: 127.0.0.1:2183    # zookeeper协议配置</span><br><span class="line">    address: 192.168.2.113:2183    # zookeeper协议配置 测试ip连接 ，zk是否支持远程调用</span><br><span class="line">  interface:</span><br><span class="line">    version: 1.0.0  # 接口版本号</span><br><span class="line">  annotation:</span><br><span class="line">    package: com.dist  # dubbo注解扫描包 ，注意更改成功自己项目的java文件路径 ，否则注册不到服务</span><br><span class="line">  consumer:</span><br><span class="line">    timeout: 50000  # 超时时间</span><br><span class="line">    check: false   # check校验：闭所有服务的启动时检查</span><br><span class="line">    version: 1.0.0 # dubbo默认版本号 ，在url显示为default.version&#x3D;xxx</span><br></pre></td></tr></table></figure><h6 id="3）-web层-消费者-spring-dubbo-consumer-xml"><a href="#3）-web层-消费者-spring-dubbo-consumer-xml" class="headerlink" title="3）.web层(消费者)- spring-dubbo-consumer.xml"></a>3）.web层(消费者)- spring-dubbo-consumer.xml</h6><p>将spring-dubbo-consumer.xml放置resources/config下</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:dubbo</span>=<span class="string">" http://code.alibabatech.com/schema/dubbo"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">" http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://code.alibabatech.com/schema/dubbo</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context/spring-context.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--加载 application*.yml 资源--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">"classpath:application*.yml"</span> <span class="attr">ignore-unresolvable</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--消费方应用名 ，用于计算依赖关系 ，不是匹配条件 ，不要与提供方一样--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:application</span> <span class="attr">name</span>=<span class="string">"$&#123;dubbo.application.name&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--zk 注册中心暴露服务地址 ，协议--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:registry</span> <span class="attr">id</span>=<span class="string">"zk"</span> <span class="attr">address</span>=<span class="string">"$&#123;dubbo.registry.address&#125;"</span> <span class="attr">protocol</span>=<span class="string">"$&#123;dubbo.registry.protocol&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--dubbo 扫描包位置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:annotation</span> <span class="attr">package</span>=<span class="string">"$&#123;dubbo.annotation.package&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--dubbo 版本 ，超时 ，check校验：关闭所有服务的启动时检查--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:consumer</span> <span class="attr">version</span>=<span class="string">"$&#123;dubbo.consumer.version&#125;"</span> <span class="attr">timeout</span>=<span class="string">"$&#123;dubbo.consumer.timeout&#125;"</span> <span class="attr">check</span>=<span class="string">"$&#123;dubbo.consumer.check&#125;"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><h6 id="4）-web层-消费者-需要引入api层-公共接口-依赖jar"><a href="#4）-web层-消费者-需要引入api层-公共接口-依赖jar" class="headerlink" title="4）.web层(消费者)- 需要引入api层(公共接口)依赖jar"></a>4）.web层(消费者)- 需要引入api层(公共接口)依赖jar</h6><p>在pom.xml添加</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--api依赖--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.dist&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;springboot-test-api&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.0-SNAPSHOT&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><h6 id="5）-web层-消费者-测试代码："><a href="#5）-web层-消费者-测试代码：" class="headerlink" title="5）.web层(消费者)- 测试代码："></a>5）.web层(消费者)- 测试代码：</h6><p>创建DubboTestService.java 接口</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DubboTestService</span> </span>&#123;</span><br><span class="line">	<span class="function">String <span class="title">getData</span><span class="params">(String data)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="service层-提供者-配置"><a href="#service层-提供者-配置" class="headerlink" title="service层(提供者)配置"></a>service层(提供者)配置</h5><h6 id="1-service层-提供者-pom-xml"><a href="#1-service层-提供者-pom-xml" class="headerlink" title="1).service层(提供者)- pom.xml"></a>1).service层(提供者)- pom.xml</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--dubbo--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>dubbo<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.101tec<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zkclient<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--dubbo-zk-curator--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-framework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><h6 id="2-service层-提供者-yml文件配置"><a href="#2-service层-提供者-yml文件配置" class="headerlink" title="2).service层(提供者)- *.yml文件配置"></a>2).service层(提供者)- *.yml文件配置</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># dubbo配置</span></span><br><span class="line"><span class="attr">dubbo:</span></span><br><span class="line">  <span class="attr">application:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">provider</span>  <span class="comment"># 提供方应用名 ，用于计算依赖关系 ，不要与消费方一样</span></span><br><span class="line">  <span class="attr">registry:</span></span><br><span class="line">    <span class="attr">protocol:</span> <span class="string">zookeeper</span>  <span class="comment">#dubbo/zookeeper 协议</span></span><br><span class="line">    <span class="attr">address:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:2183</span>  <span class="comment"># zookeeper协议配置方式</span></span><br><span class="line">    <span class="comment">#address: 192.168.2.113:2183    # zookeeper协议配置 测试ip连接 ，zk是否支持远程调用</span></span><br><span class="line">    <span class="comment">#address: zookeeper://127.0.0.1:2183  # dubbo协议配置</span></span><br><span class="line">  <span class="attr">protocol:</span></span><br><span class="line">    <span class="attr">port:</span> <span class="number">30103</span>  <span class="comment"># dubbo协议缺省port端口20880 ，多个提供者会冲突</span></span><br><span class="line">  <span class="attr">annotation:</span></span><br><span class="line">    <span class="attr">package:</span> <span class="string">com.dist.server</span>  <span class="comment"># dubbo注解扫描包 ，注意更改成功自己项目的java文件路径 ，否则注册不到服务</span></span><br><span class="line">  <span class="attr">provider:</span></span><br><span class="line">    <span class="attr">version:</span> <span class="number">1.0</span><span class="number">.0</span> <span class="comment">#dubbo默认版本号 ，在url显示为default.version=xxx</span></span><br></pre></td></tr></table></figure><h6 id="3-service层-提供者-spring-dubbo-provider-xml"><a href="#3-service层-提供者-spring-dubbo-provider-xml" class="headerlink" title="3).service层(提供者)- spring-dubbo-provider.xml"></a>3).service层(提供者)- spring-dubbo-provider.xml</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:dubbo</span>=<span class="string">" http://code.alibabatech.com/schema/dubbo"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">" http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://code.alibabatech.com/schema/dubbo</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://code.alibabatech.com/schema/dubbo/dubbo.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context/spring-context.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--加载 application*.yml 资源--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">"classpath:application*.yml"</span> <span class="attr">ignore-unresolvable</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--提供方应用名 ，用于计算依赖关系 ，不是匹配条件 ，不要与消费方一样--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:application</span> <span class="attr">name</span>=<span class="string">"$&#123;dubbo.application.name&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--zk 注册中心暴露服务地址,协议：dubbo/zookeeper,dubbo协议缺省port端口20880 ，多个提供者会冲突 添加 port="$&#123;dubbo.protocol.port&#125;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:registry</span> <span class="attr">address</span>=<span class="string">"$&#123;dubbo.registry.address&#125;"</span> <span class="attr">protocol</span>=<span class="string">"$&#123;dubbo.registry.protocol&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--dubbo 版本 ，超时--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:provider</span> <span class="attr">version</span>=<span class="string">"$&#123;dubbo.provider.version&#125;"</span> <span class="attr">timeout</span>=<span class="string">"50000"</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--dubbo 扫描包位置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dubbo:annotation</span> <span class="attr">package</span>=<span class="string">"$&#123;dubbo.annotation.package&#125;"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><h6 id="4-service层-提供者-需要引入api层-公共接口-依赖jar"><a href="#4-service层-提供者-需要引入api层-公共接口-依赖jar" class="headerlink" title="4).service层(提供者)- 需要引入api层(公共接口)依赖jar"></a>4).service层(提供者)- 需要引入api层(公共接口)依赖jar</h6><p>在pom.xml添加</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--api依赖--&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;com.dist&lt;&#x2F;groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;springboot-test-api&lt;&#x2F;artifactId&gt;</span><br><span class="line">    &lt;version&gt;1.0-SNAPSHOT&lt;&#x2F;version&gt;</span><br><span class="line">&lt;&#x2F;dependency&gt;</span><br></pre></td></tr></table></figure><h6 id="5-service层-提供者-测试代码："><a href="#5-service层-提供者-测试代码：" class="headerlink" title="5).service层(提供者)- 测试代码："></a>5).service层(提供者)- 测试代码：</h6><p>DubboTestServiceImpl.java实现类</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.alibaba.dubbo.config.annotation.Service;</span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DubboTestServiceImpl</span> <span class="keyword">implements</span> <span class="title">DubboTestService</span> </span>&#123;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getData</span><span class="params">(String data)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"service层返回的data数据:"</span>+data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h5 id="api层-公共接口"><a href="#api层-公共接口" class="headerlink" title="api层(公共接口)"></a>api层(公共接口)</h5><p>api层提供公共接口 ，web层调用和service层实现 ，需要两者都引入api层的依赖jar：</p><p>DubboTestService.java 公共接口</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DubboTestService</span> </span>&#123;</span><br><span class="line">	<span class="function">String <span class="title">getData</span><span class="params">(String data)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="3、伪集群测试-分布式-dubbo"><a href="#3、伪集群测试-分布式-dubbo" class="headerlink" title="3、伪集群测试-分布式(dubbo)"></a>3、伪集群测试-分布式(dubbo)</h4><p>在 <strong>2、单机测试-分布式(dubbo)</strong> 基础上更改 *.yml 配置:</p><h5 id="1-web层-消费者-yml文件配置：zookeeper集群配置"><a href="#1-web层-消费者-yml文件配置：zookeeper集群配置" class="headerlink" title="1.web层(消费者)- *.yml文件配置：zookeeper集群配置"></a>1.web层(消费者)- *.yml文件配置：zookeeper集群配置</h5><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># dubbo配置</span></span><br><span class="line"><span class="attr">dubbo:</span></span><br><span class="line">  <span class="attr">application:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">consumer</span></span><br><span class="line">  <span class="attr">registry:</span></span><br><span class="line">    <span class="attr">protocol:</span> <span class="string">zookeeper</span>   <span class="comment">#dubbo/zookeeper 协议</span></span><br><span class="line">    <span class="comment">#address: 127.0.0.1:2183    # zookeeper协议配置</span></span><br><span class="line">    <span class="comment">#address: 192.168.2.113:2183    # zookeeper协议配置 测试ip连接 ，zk是否支持远程调用</span></span><br><span class="line">    <span class="comment">#address: zookeeper://127.0.0.1:2183  # dubbo协议配置</span></span><br><span class="line">    <span class="comment">#address: zookeeper://127.0.0.1:2183?backup=127.0.0.1:2184,127.0.0.1:2185  # dubbo协议配置集群-zk主从配置方法</span></span><br><span class="line">    <span class="attr">address:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:2183,127.0.0.1:2184,127.0.0.1:2185</span>  <span class="comment"># zookeeper协议配置集群-zk非主从配置方法</span></span><br><span class="line">  <span class="attr">interface:</span></span><br><span class="line">    <span class="attr">version:</span> <span class="number">1.0</span><span class="number">.0</span></span><br><span class="line">  <span class="attr">annotation:</span></span><br><span class="line">    <span class="attr">package:</span> <span class="string">com.dist</span>  <span class="comment"># 扫描包</span></span><br><span class="line">  <span class="attr">consumer:</span></span><br><span class="line">    <span class="attr">timeout:</span> <span class="number">50000</span></span><br><span class="line">    <span class="attr">check:</span> <span class="literal">false</span></span><br><span class="line">    <span class="attr">version:</span> <span class="number">1.0</span><span class="number">.0</span> <span class="comment">#dubbo默认版本号 ，在url显示为default.version=xxx</span></span><br></pre></td></tr></table></figure><h5 id="2-service层-提供者-yml配置：集群配置"><a href="#2-service层-提供者-yml配置：集群配置" class="headerlink" title="2.service层(提供者)- *.yml配置：集群配置"></a>2.service层(提供者)- *.yml配置：集群配置</h5><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># dubbo配置</span></span><br><span class="line"><span class="attr">dubbo:</span></span><br><span class="line">  <span class="attr">application:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">provider</span></span><br><span class="line">  <span class="attr">registry:</span></span><br><span class="line">    <span class="attr">protocol:</span> <span class="string">zookeeper</span>  <span class="comment">#dubbo/zookeeper 协议</span></span><br><span class="line">    <span class="comment">#address: 127.0.0.1:2183  # zookeeper协议配置方式</span></span><br><span class="line">    <span class="comment">#address: 192.168.2.113:2183    # zookeeper协议配置 测试ip连接 ，zk是否支持远程调用</span></span><br><span class="line">    <span class="comment">#address: zookeeper://127.0.0.1:2183  # dubbo协议配置</span></span><br><span class="line">    <span class="comment">#address: zookeeper://127.0.0.1:2183?backup=127.0.0.1:2184,127.0.0.1:2185  # dubbo协议配置集群-zk主从配置方法</span></span><br><span class="line">    <span class="attr">address:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span><span class="string">:2183,127.0.0.1:2184,127.0.0.1:2185</span>  <span class="comment"># zookeeper协议配置集群-zk非主从配置方法</span></span><br><span class="line">  <span class="attr">protocol:</span></span><br><span class="line">    <span class="attr">port:</span> <span class="number">30103</span></span><br><span class="line">  <span class="attr">annotation:</span></span><br><span class="line">    <span class="attr">package:</span> <span class="string">com.dist.server</span></span><br><span class="line">  <span class="attr">provider:</span></span><br><span class="line">    <span class="attr">version:</span> <span class="number">1.0</span><span class="number">.0</span> <span class="comment">#dubbo默认版本号 ，在url显示为default.version=xxx</span></span><br></pre></td></tr></table></figure><p><strong>到这里伪集群测试-配置完成！</strong></p><blockquote><p>备注：</p><ul><li><p>web层协议和service协议最好配置一样 ，protocol: dubbo/zookeeper # 协议当然两者配置不一致也可以调用</p><hr></li></ul></blockquote><h3 id="六、Zookeeper安全认证"><a href="#六、Zookeeper安全认证" class="headerlink" title="六、Zookeeper安全认证"></a>六、Zookeeper安全认证</h3><h4 id="1、为什么Zookeeper要安全认证-？"><a href="#1、为什么Zookeeper要安全认证-？" class="headerlink" title="1、为什么Zookeeper要安全认证 ？"></a>1、为什么Zookeeper要安全认证 ？</h4><p>1).服务都是在内网 ，Zookeeper集群配置都是走的内网IP ，外网不开放相关端口 ，不需要zookeeper对外开放。但是可能由于业务升级 ，例如购置了阿里云的服务 ，需要对外开放Zookeeper服务 ，就需要对zookeeper进行安全认证了。</p><p>2).Zookeeper 未授权访问(中危 ，3处)<br><img src="https://upload-images.jianshu.io/upload_images/15645795-2f22ce963b250b12.png" alt="zk认证1.png"></p><h4 id="2-ACL认证的简介"><a href="#2-ACL认证的简介" class="headerlink" title="2.ACL认证的简介"></a>2.ACL认证的简介</h4><p>首先说明一下为什么需要ACL</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">简单来说 :在通常情况下,zookeeper允许未经授权的访问,因此在安全漏洞扫描中暴漏未授权访问漏洞。这在一些监控很严的系统中是不被允许的,所以需要ACL来控制权限.</span><br></pre></td></tr></table></figure><p>既然需要ACL来控制权限,那么Zookeeper的权限有哪些呢?<br>权限包括以下几种:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CREATE: 能创建子节点</span><br><span class="line">READ：能获取节点数据和列出其子节点</span><br><span class="line">WRITE: 能设置节点数据</span><br><span class="line">DELETE: 能删除子节点</span><br><span class="line">ADMIN: 能设置权限</span><br></pre></td></tr></table></figure><p>说到权限,介绍一下zookeeper的四种认证方式:</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">world：默认方式 ，相当于全世界都能访问</span><br><span class="line">auth：代表已经认证通过的用户(cli中可以通过addauth digest user:pwd 来添加当前上下文中的授权用户)</span><br><span class="line">digest：即用户名:密码这种方式认证 ，这也是业务系统中最常用的</span><br><span class="line">ip：使用Ip地址认证</span><br></pre></td></tr></table></figure><p>ACL基本介绍就到这里。</p><h4 id="3-没有ACL认证时zookeeper的操作"><a href="#3-没有ACL认证时zookeeper的操作" class="headerlink" title="3.没有ACL认证时zookeeper的操作"></a>3.没有ACL认证时zookeeper的操作</h4><p>直接上代码 : 更改一下服务器地址和端口号即可!<br>pom.xml</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">   <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.CreateMode;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.KeeperException;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.WatchedEvent;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.Watcher;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.ZooDefs.Ids;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.ZooKeeper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZkConn</span> </span>&#123;</span><br><span class="line">	 <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span></span><br><span class="line"><span class="function">             <span class="keyword">throws</span> IOException, KeeperException, InterruptedException </span>&#123;</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      *  创建一个与服务器的连接</span></span><br><span class="line"><span class="comment">      *  参数一：服务器地址和端口号（该端口号值服务器允许客户端连接的端口号）</span></span><br><span class="line"><span class="comment">      *  参数二：连接会话超时时间</span></span><br><span class="line"><span class="comment">      *  参数三：观察者 ，连接成功会触发该观察者。不过只会触发一次。</span></span><br><span class="line"><span class="comment">      *      该Watcher会获取各种事件的通知</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     ZooKeeper zk = <span class="keyword">new</span> ZooKeeper(<span class="string">"node005:4180"</span>, <span class="number">60000</span>, <span class="keyword">new</span> Watcher() &#123;</span><br><span class="line">         <span class="comment">// 监控所有被触发的事件</span></span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent event)</span> </span>&#123;</span><br><span class="line">             System.out.println(<span class="string">"监控所有被触发的事件:EVENT:"</span> + event.getType());</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;);</span><br><span class="line">     System.out.println(<span class="string">"*******************************************************"</span>);</span><br><span class="line">     <span class="comment">// 查看根节点的子节点</span></span><br><span class="line">     System.out.println(<span class="string">"查看根节点的子节点:ls / =&gt; "</span> + zk.getChildren(<span class="string">"/"</span>, <span class="keyword">true</span>));</span><br><span class="line">     System.out.println(<span class="string">"*******************************************************"</span>);</span><br><span class="line">     <span class="comment">// 创建一个目录节点</span></span><br><span class="line">     <span class="keyword">if</span> (zk.exists(<span class="string">"/node"</span>, <span class="keyword">true</span>) == <span class="keyword">null</span>) &#123;</span><br><span class="line">         <span class="comment">/**</span></span><br><span class="line"><span class="comment">          * 参数一：路径地址</span></span><br><span class="line"><span class="comment">          * 参数二：想要保存的数据 ，需要转换成字节数组</span></span><br><span class="line"><span class="comment">          * 参数三：ACL访问控制列表（Access control list）,</span></span><br><span class="line"><span class="comment">          *      参数类型为ArrayList&lt;ACL&gt; ，Ids接口提供了一些默认的值可以调用。</span></span><br><span class="line"><span class="comment">          *      OPEN_ACL_UNSAFE     This is a completely open ACL </span></span><br><span class="line"><span class="comment">          *                          这是一个完全开放的ACL ，不安全</span></span><br><span class="line"><span class="comment">          *      CREATOR_ALL_ACL     This ACL gives the</span></span><br><span class="line"><span class="comment">          *                           creators authentication id's all permissions.</span></span><br><span class="line"><span class="comment">          *                          这个ACL赋予那些授权了的用户具备权限</span></span><br><span class="line"><span class="comment">          *      READ_ACL_UNSAFE     This ACL gives the world the ability to read.</span></span><br><span class="line"><span class="comment">          *                          这个ACL赋予用户读的权限 ，也就是获取数据之类的权限。</span></span><br><span class="line"><span class="comment">          * 参数四：创建的节点类型。枚举值CreateMode</span></span><br><span class="line"><span class="comment">          *      PERSISTENT (0, false, false)</span></span><br><span class="line"><span class="comment">          *      PERSISTENT_SEQUENTIAL (2, false, true)</span></span><br><span class="line"><span class="comment">          *          这两个类型创建的都是持久型类型节点 ，回话结束之后不会自动删除。</span></span><br><span class="line"><span class="comment">          *          区别在于 ，第二个类型所创建的节点名后会有一个单调递增的数值</span></span><br><span class="line"><span class="comment">          *      EPHEMERAL (1, true, false)</span></span><br><span class="line"><span class="comment">          *      EPHEMERAL_SEQUENTIAL (3, true, true)</span></span><br><span class="line"><span class="comment">          *          这两个类型所创建的是临时型类型节点 ，在回话结束之后 ，自动删除。</span></span><br><span class="line"><span class="comment">          *          区别在于 ，第二个类型所创建的临时型节点名后面会有一个单调递增的数值。</span></span><br><span class="line"><span class="comment">          * 最后create()方法的返回值是创建的节点的实际路径</span></span><br><span class="line"><span class="comment">          */</span></span><br><span class="line">         zk.create(<span class="string">"/node"</span>, <span class="string">"conan"</span>.getBytes(),</span><br><span class="line">                 Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);</span><br><span class="line">         System.out.println(<span class="string">"创建一个目录节点:create /node conan"</span>);</span><br><span class="line">         <span class="comment">/**</span></span><br><span class="line"><span class="comment">          *  查看/node节点数据 ，这里应该输出"conan"</span></span><br><span class="line"><span class="comment">          *  参数一：获取节点的路径</span></span><br><span class="line"><span class="comment">          *  参数二：说明是否需要观察该节点 ，设置为true ，则设定共享默认的观察器</span></span><br><span class="line"><span class="comment">          *  参数三：stat类 ，保存节点的信息。例如数据版本信息 ，创建时间 ，修改时间等信息</span></span><br><span class="line"><span class="comment">          */</span></span><br><span class="line">         System.out.println(<span class="string">"查看/node节点数据:get /node =&gt; "</span></span><br><span class="line">                 + <span class="keyword">new</span> String(zk.getData(<span class="string">"/node"</span>, <span class="keyword">false</span>, <span class="keyword">null</span>)));</span><br><span class="line">         <span class="comment">/**</span></span><br><span class="line"><span class="comment">          * 查看根节点</span></span><br><span class="line"><span class="comment">          * 在此查看根节点的值 ，这里应该输出上面所创建的/node节点</span></span><br><span class="line"><span class="comment">          */</span></span><br><span class="line">         System.out.println(<span class="string">"查看根节点:ls / =&gt; "</span> + zk.getChildren(<span class="string">"/"</span>, <span class="keyword">true</span>));</span><br><span class="line">     &#125;</span><br><span class="line">     System.out.println(<span class="string">"*******************************************************"</span>);</span><br><span class="line">     <span class="comment">// 创建一个子目录节点</span></span><br><span class="line">     <span class="keyword">if</span> (zk.exists(<span class="string">"/node/sub1"</span>, <span class="keyword">true</span>) == <span class="keyword">null</span>) &#123;</span><br><span class="line">         zk.create(<span class="string">"/node/sub1"</span>, <span class="string">"sub1"</span>.getBytes(),</span><br><span class="line">                 Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);</span><br><span class="line">         System.out.println(<span class="string">"创建一个子目录节点:create /node/sub1 sub1"</span>);</span><br><span class="line">         <span class="comment">// 查看node节点</span></span><br><span class="line">         System.out.println(<span class="string">"查看node节点:ls /node =&gt; "</span></span><br><span class="line">                 + zk.getChildren(<span class="string">"/node"</span>, <span class="keyword">true</span>));</span><br><span class="line">     &#125;</span><br><span class="line">     System.out.println(<span class="string">"*******************************************************"</span>);</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">      *  修改节点数据</span></span><br><span class="line"><span class="comment">      *  修改的数据会覆盖上次所设置的数据</span></span><br><span class="line"><span class="comment">      *  setData()方法参数一、参数二不多说 ，与上面类似。</span></span><br><span class="line"><span class="comment">      *  参数三：数值型。需要传入该界面的数值类型版本号！！！</span></span><br><span class="line"><span class="comment">      *      该信息可以通过Stat类获取 ，也可以通过命令行获取。</span></span><br><span class="line"><span class="comment">      *      如果该值设置为-1 ，就是忽视版本匹配 ，直接设置节点保存的值。</span></span><br><span class="line"><span class="comment">      */</span></span><br><span class="line">     <span class="keyword">if</span> (zk.exists(<span class="string">"/node"</span>, <span class="keyword">true</span>) != <span class="keyword">null</span>) &#123;</span><br><span class="line">         zk.setData(<span class="string">"/node"</span>, <span class="string">"changed"</span>.getBytes(), -<span class="number">1</span>);</span><br><span class="line">         <span class="comment">// 查看/node节点数据</span></span><br><span class="line">         System.out.println(<span class="string">"修改节点数据:get /node =&gt; "</span></span><br><span class="line">                 + <span class="keyword">new</span> String(zk.getData(<span class="string">"/node"</span>, <span class="keyword">false</span>, <span class="keyword">null</span>)));</span><br><span class="line">     &#125;</span><br><span class="line">     System.out.println(<span class="string">"*******************************************************"</span>);</span><br><span class="line">     <span class="comment">// 删除节点</span></span><br><span class="line">     <span class="keyword">if</span> (zk.exists(<span class="string">"/node/sub1"</span>, <span class="keyword">true</span>) != <span class="keyword">null</span>) &#123;</span><br><span class="line">         zk.delete(<span class="string">"/node/sub1"</span>, -<span class="number">1</span>);</span><br><span class="line">         zk.delete(<span class="string">"/node"</span>, -<span class="number">1</span>);</span><br><span class="line">         <span class="comment">// 查看根节点</span></span><br><span class="line">         System.out.println(<span class="string">"删除节点:ls / =&gt; "</span> + zk.getChildren(<span class="string">"/"</span>, <span class="keyword">true</span>));</span><br><span class="line">     &#125;</span><br><span class="line">     <span class="comment">// 关闭连接</span></span><br><span class="line">     zk.close();</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><strong>认证只是针对一个节点</strong></p><blockquote><ul><li><p><strong>认证只是针对一个节点</strong></p></li><li><p><strong>ACL</strong>【Access Control List】 ，ZooKeeper作为一个分布式协调框架 ，其内部存储的都是一些关乎分布式系统运行时状态的元数据 ，尤其是涉及到一些分布式锁 ，Master选举和协调等应用场景。我们需要有效的保障ZooKeeper中的数据安全 ，ZooKeeper提供了三种模式。权限模式、授权对象、权限。</p></li><li><p><strong>权限模式</strong>：Scheme ，开发人员最多使用的如下四种权限模式：<br>IP：IP模式通过IP地址粒度来进行控制权限 ，例如配置了：IP ，192.168.1.107即表示权限控制都是针对这个IP地址的 ，同时也支持按网段分配 ，比如：192.168.1.*</p><p>Digest：digest是最常用的权限控制模式 ，也更符合我们对权限控制的认识 ，其类似于“username：password”形式的权限标识进行权限配置。ZooKeeper会对形式的权限标识先后进行两次编码处理 ，分别是SHA-1加密算法 ，BASE64编码</p><p>World：World是一直最开放的权限控制模式。这种控制模式可以看做为特殊的Digest ，它仅仅是一个标识而已</p><p>Super：超级用户模式 ，在超级用户模式下可以对ZooKeeper任意进行操作</p></li><li><p><strong>授权对象</strong>：指的是权限赋予的用户或者一个指定的实体 ，例如IP地址或者机器等。在不同的模式下 ，授权对象是不同的。这种模式和权限对象一一对应</p></li><li><p><strong>权限</strong>：权限就是指那些通过权限检测后可以被允许执行的操作 ，在ZooKeeper中 ，对数据的操作权限分为以下五个大类：CREATE、DELETE、READ、WRITE、ADMIN</p><hr></li></ul></blockquote><h4 id="4-有ACL认证时zookeeper的操作测试："><a href="#4-有ACL认证时zookeeper的操作测试：" class="headerlink" title="4.有ACL认证时zookeeper的操作测试："></a>4.有ACL认证时zookeeper的操作测试：</h4><p>pom.xml</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.8<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><p>代码类:Zookeeper 节点授权</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.dist;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.*;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.data.ACL;</span><br><span class="line"><span class="keyword">import</span> org.apache.zookeeper.data.Stat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.CountDownLatch;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.atomic.AtomicInteger;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**Zookeeper 节点授权</span></span><br><span class="line"><span class="comment"> * 这里测试的认证方式：digest  相当于  user:pass 认证</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@data</span> 2019/8/22 15:29</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ZookeeperAuth</span> <span class="keyword">implements</span> <span class="title">Watcher</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 连接地址 */</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String CONNECT_ADDR = <span class="string">"127.0.0.1:2183"</span>;</span><br><span class="line">    <span class="comment">/** 测试路径 */</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String PATH = <span class="string">"/testAuth"</span>;</span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String PATH_DEL = <span class="string">"/testAuth/delNode"</span>;</span><br><span class="line">    <span class="comment">/** 认证类型 */</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String authentication_type = <span class="string">"digest"</span>;</span><br><span class="line">    <span class="comment">/** 认证正确方法 */</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String correctAuthentication = <span class="string">"123456"</span>;</span><br><span class="line">    <span class="comment">/** 认证错误方法 */</span></span><br><span class="line">    <span class="keyword">final</span> <span class="keyword">static</span> String badAuthentication = <span class="string">"654321"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> ZooKeeper zk = <span class="keyword">null</span>;</span><br><span class="line">    <span class="comment">/** 计时器 */</span></span><br><span class="line">    AtomicInteger seq = <span class="keyword">new</span> AtomicInteger();</span><br><span class="line">    <span class="comment">/** 标识 */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String LOG_PREFIX_OF_MAIN = <span class="string">"【Main】"</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> CountDownLatch connectedSemaphore = <span class="keyword">new</span> CountDownLatch(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent event)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Thread.sleep(<span class="number">200</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (event==<span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 连接状态</span></span><br><span class="line">        Event.KeeperState keeperState = event.getState();</span><br><span class="line">        <span class="comment">// 事件类型</span></span><br><span class="line">        Event.EventType eventType = event.getType();</span><br><span class="line">        <span class="comment">// 受影响的path</span></span><br><span class="line">        String path = event.getPath();</span><br><span class="line"></span><br><span class="line">        String logPrefix = <span class="string">"【Watcher-"</span> + <span class="keyword">this</span>.seq.incrementAndGet() + <span class="string">"】"</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(logPrefix + <span class="string">"收到Watcher通知"</span>);</span><br><span class="line">        System.out.println(logPrefix + <span class="string">"连接状态:\t"</span> + keeperState.toString());</span><br><span class="line">        System.out.println(logPrefix + <span class="string">"事件类型:\t"</span> + eventType.toString());</span><br><span class="line">        <span class="keyword">if</span> (Event.KeeperState.SyncConnected == keeperState) &#123;</span><br><span class="line">            <span class="comment">// 成功连接上ZK服务器</span></span><br><span class="line">            <span class="keyword">if</span> (Event.EventType.None == eventType) &#123;</span><br><span class="line">                System.out.println(logPrefix + <span class="string">"成功连接上ZK服务器"</span>);</span><br><span class="line">                connectedSemaphore.countDown();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (Event.KeeperState.Disconnected == keeperState) &#123;</span><br><span class="line">            System.out.println(logPrefix + <span class="string">"与ZK服务器断开连接"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (Event.KeeperState.AuthFailed == keeperState) &#123;</span><br><span class="line">            System.out.println(logPrefix + <span class="string">"权限检查失败"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (Event.KeeperState.Expired == keeperState) &#123;</span><br><span class="line">            System.out.println(logPrefix + <span class="string">"会话失效"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">"--------------------------------------------"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 创建ZK连接</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> connectString</span></span><br><span class="line"><span class="comment">     *            ZK服务器地址列表</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> sessionTimeout</span></span><br><span class="line"><span class="comment">     *            Session超时时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">createConnection</span><span class="params">(String connectString, <span class="keyword">int</span> sessionTimeout)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.releaseConnection();</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            zk = <span class="keyword">new</span> ZooKeeper(connectString, sessionTimeout, <span class="keyword">this</span>);</span><br><span class="line">            <span class="comment">//添加节点授权</span></span><br><span class="line">            zk.addAuthInfo(authentication_type,correctAuthentication.getBytes());</span><br><span class="line">            System.out.println(LOG_PREFIX_OF_MAIN + <span class="string">"开始连接ZK服务器"</span>);</span><br><span class="line">            <span class="comment">//倒数等待</span></span><br><span class="line">            connectedSemaphore.await();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 关闭ZK连接</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">releaseConnection</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.zk!=<span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">this</span>.zk.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * &lt;B&gt;方法名称：&lt;/B&gt;测试函数&lt;BR&gt;</span></span><br><span class="line"><span class="comment">     * &lt;B&gt;概要说明：&lt;/B&gt;测试认证&lt;BR&gt;</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> args</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        ZookeeperAuth testAuth = <span class="keyword">new</span> ZookeeperAuth();</span><br><span class="line">        testAuth.createConnection(CONNECT_ADDR,<span class="number">2000</span>);</span><br><span class="line">        List&lt;ACL&gt; acls = <span class="keyword">new</span> ArrayList&lt;ACL&gt;(<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">for</span> (ACL ids_acl : ZooDefs.Ids.CREATOR_ALL_ACL) &#123;</span><br><span class="line">            acls.add(ids_acl);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            zk.create(PATH, <span class="string">"init content"</span>.getBytes(), acls, CreateMode.PERSISTENT);</span><br><span class="line">            System.out.println(<span class="string">"使用授权key："</span> + correctAuthentication + <span class="string">"创建节点："</span>+ PATH + <span class="string">", 初始内容是: init content"</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            zk.create(PATH_DEL, <span class="string">"will be deleted! "</span>.getBytes(), acls, CreateMode.PERSISTENT);</span><br><span class="line">            System.out.println(<span class="string">"使用授权key："</span> + correctAuthentication + <span class="string">"创建节点："</span>+ PATH_DEL + <span class="string">", 初始内容是: will be deleted!"</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取数据</span></span><br><span class="line">        getDataByNoAuthentication();  <span class="comment">//获取数据：不采用密码</span></span><br><span class="line">        getDataByBadAuthentication(); <span class="comment">//获取数据：采用错误的密码 </span></span><br><span class="line">        getDataByCorrectAuthentication();  <span class="comment">//采用正确的密码</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 更新数据</span></span><br><span class="line">        updateDataByNoAuthentication();  <span class="comment">//更新数据：不采用密码</span></span><br><span class="line">        updateDataByBadAuthentication();  <span class="comment">//更新数据：采用错误的密码</span></span><br><span class="line">        updateDataByCorrectAuthentication();  <span class="comment">//更新数据：采用正确的密码</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 删除数据</span></span><br><span class="line">        deleteNodeByNoAuthentication();  <span class="comment">//不使用密码 删除节点</span></span><br><span class="line">        deleteNodeByBadAuthentication(); <span class="comment">//采用错误的密码删除节点</span></span><br><span class="line">        deleteNodeByCorrectAuthentication();  <span class="comment">//使用正确的密码删除节点</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//线程等待</span></span><br><span class="line">        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用正确的密码删除父节点</span></span><br><span class="line">        deleteParent();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">//释放连接</span></span><br><span class="line">        testAuth.releaseConnection();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 获取数据：采用错误的密码 */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">getDataByBadAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String prefix = <span class="string">"[使用错误的授权信息]"</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            ZooKeeper badzk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            <span class="comment">//授权</span></span><br><span class="line">            badzk.addAuthInfo(authentication_type,badAuthentication.getBytes());</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            System.out.println(prefix + <span class="string">"获取数据："</span> + PATH);</span><br><span class="line">            System.out.println(prefix + <span class="string">"成功获取数据："</span> + badzk.getData(PATH, <span class="keyword">false</span>, <span class="keyword">null</span>));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"获取数据失败 ，原因："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 获取数据：不采用密码 */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">getDataByNoAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String prefix = <span class="string">"[不使用任何授权信息]"</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"获取数据："</span> + PATH);</span><br><span class="line">            ZooKeeper nozk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            System.out.println(prefix + <span class="string">"成功获取数据："</span> + nozk.getData(PATH, <span class="keyword">false</span>, <span class="keyword">null</span>));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"获取数据失败 ，原因："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 采用正确的密码 */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">getDataByCorrectAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String prefix = <span class="string">"[使用正确的授权信息]"</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"获取数据："</span> + PATH);</span><br><span class="line"></span><br><span class="line">            System.out.println(prefix + <span class="string">"成功获取数据："</span> + zk.getData(PATH, <span class="keyword">false</span>, <span class="keyword">null</span>));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"获取数据失败 ，原因："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新数据：不采用密码</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">updateDataByNoAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[不使用任何授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(prefix + <span class="string">"更新数据： "</span> + PATH);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            ZooKeeper nozk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            Stat stat = nozk.exists(PATH, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                nozk.setData(PATH, prefix.getBytes(), -<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"更新成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"更新失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新数据：采用错误的密码</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">updateDataByBadAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[使用错误的授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(prefix + <span class="string">"更新数据："</span> + PATH);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            ZooKeeper badzk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            <span class="comment">//授权</span></span><br><span class="line">            badzk.addAuthInfo(authentication_type,badAuthentication.getBytes());</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            Stat stat = badzk.exists(PATH, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                badzk.setData(PATH, prefix.getBytes(), -<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"更新成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"更新失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更新数据：采用正确的密码</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">updateDataByCorrectAuthentication</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[使用正确的授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(prefix + <span class="string">"更新数据："</span> + PATH);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Stat stat = zk.exists(PATH, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                zk.setData(PATH, prefix.getBytes(), -<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"更新成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"更新失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 不使用密码 删除节点</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteNodeByNoAuthentication</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[不使用任何授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"删除节点："</span> + PATH_DEL);</span><br><span class="line">            ZooKeeper nozk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            Stat stat = nozk.exists(PATH_DEL, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                nozk.delete(PATH_DEL,-<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"删除成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"删除失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 采用错误的密码删除节点</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteNodeByBadAuthentication</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[使用错误的授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"删除节点："</span> + PATH_DEL);</span><br><span class="line">            ZooKeeper badzk = <span class="keyword">new</span> ZooKeeper(CONNECT_ADDR, <span class="number">2000</span>, <span class="keyword">null</span>);</span><br><span class="line">            <span class="comment">//授权</span></span><br><span class="line">            badzk.addAuthInfo(authentication_type,badAuthentication.getBytes());</span><br><span class="line">            Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">            Stat stat = badzk.exists(PATH_DEL, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                badzk.delete(PATH_DEL, -<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"删除成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.err.println(prefix + <span class="string">"删除失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用正确的密码删除节点</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteNodeByCorrectAuthentication</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">        String prefix = <span class="string">"[使用正确的授权信息]"</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"删除节点："</span> + PATH_DEL);</span><br><span class="line">            Stat stat = zk.exists(PATH_DEL, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat!=<span class="keyword">null</span>) &#123;</span><br><span class="line">                zk.delete(PATH_DEL, -<span class="number">1</span>);</span><br><span class="line">                System.out.println(prefix + <span class="string">"删除成功"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"删除失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 使用正确的密码删除父节点</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteParent</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        String prefix = <span class="string">"[使用正确的授权信息]"</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Stat stat = zk.exists(PATH_DEL, <span class="keyword">false</span>);</span><br><span class="line">            <span class="keyword">if</span> (stat == <span class="keyword">null</span>) &#123;</span><br><span class="line">                zk.delete(PATH, -<span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            System.out.println(prefix + <span class="string">"删除父节点失败 ，原因是："</span> + e.getMessage());</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h4 id="5-zookeeper超级用户配置-Windows-Linux"><a href="#5-zookeeper超级用户配置-Windows-Linux" class="headerlink" title="5.zookeeper超级用户配置(Windows/Linux)"></a>5.zookeeper超级用户配置(Windows/Linux)</h4><ul><li><a href="https://www.jianshu.com/p/530b75674647" target="_blank" rel="noopener">Zookeeper 配置超级用户(Windows\ Linux)</a></li></ul><h3 id="七、Zookeeper-Dubbo认证"><a href="#七、Zookeeper-Dubbo认证" class="headerlink" title="七、Zookeeper+Dubbo认证"></a>七、Zookeeper+Dubbo认证</h3><blockquote><ul><li>可通过 &lt; dubbo:registry username=”admin” password=”1234” /&gt; 设置 zookeeper 登录信息</li><li>可通过 &lt; dubbo:registry group=”dubbo” /&gt; 设置 zookeeper 的根节点 ，不设置将使用无根树</li></ul><p>官网文档第五条 ，明确说明了可以通过username和 password字段设置zookeeper 登录信息。</p></blockquote><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">但是 ，如果在Zookeeper上通过digest方式设置ACL ，然后在dubbo registry上配置相应的用户、密码 ，服务就注册不到Zookeeper上了 ，会报KeeperErrorCode = NoAuth错误。</span><br><span class="line"></span><br><span class="line">看了下调用相关代码 ，发现注册服务时所传的ACL ，而配置在dubbo上的 ，没有发现被使用的地方（如果注册中心是Zookeeper的话）。</span><br></pre></td></tr></table></figure><p>但是查阅ZookeeperRegistry相关源码并没有发现相关认证的地方 ，搜遍全网很少有问类似的问题 ，这个问题似乎并没有多少人关注。</p><p><strong>大部分服务大都是部署在内网的 ，基本很少对外网开放 ，然而Dubbo的zookeeper用户权限认证貌似真的不起作用 ，如果非要对外开放只能通过iptables或者firewall进行IP Access Control ，如果是阿里云服务器的话安全组也是个不错的选择</strong></p></div><div class="popular-posts-header"><p style="font-size:18px;color:red;margin:0 0 5px 0">相关文章推荐：</p></div><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2697482281.html" rel="bookmark">Zookeeper-配置超级用户(Windows-Linux)</a></div></li></ul><div><div style="text-align:center;color:#ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div></div><div class="reward-container"><div><p style="font-size:14px;color:#34495e;margin:0 0 5px 0">赞赏一下吧～ 还可以关注公众号订阅最新内容</p></div><button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/my-images/WeChat.png" alt="宇宙小神特别萌 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/my-images/Alipay.png" alt="宇宙小神特别萌 支付宝"><p>支付宝</p></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/Zookeeper/" rel="tag"><i class="fa fa-tag"></i> Zookeeper</a></div><div class="post-nav"><div class="post-nav-item"><a href="/posts/477243062.html" rel="prev" title="ehcache、redis、memcached缓存的应用场景"><i class="fa fa-chevron-left"></i> ehcache、redis、memcached缓存的应用场景</a></div><div class="post-nav-item"><a href="/posts/228604021.html" rel="next" title="Windows-Git-安装及使用">Windows-Git-安装及使用 <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一、Zookeeper安装-单机部署"><span class="nav-text">一、Zookeeper安装(单机部署)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、下载地址："><span class="nav-text">1、下载地址：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、添加tmp文件夹"><span class="nav-text">2、添加tmp文件夹</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、打开D-zookeeper-3-4-14-conf"><span class="nav-text">3、打开D:\zookeeper-3.4.14\conf</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4、启动zookeeper"><span class="nav-text">4、启动zookeeper</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5、测试zookeeper是否可用："><span class="nav-text">5、测试zookeeper是否可用：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二、Zookeeper-伪分布式安装（集群）"><span class="nav-text">二、Zookeeper 伪分布式安装（集群）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、解压缩后的目录"><span class="nav-text">1、解压缩后的目录</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、修改配置"><span class="nav-text">2、修改配置</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加tmp文件夹"><span class="nav-text">添加tmp文件夹</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、启动zookeeper服务："><span class="nav-text">3、启动zookeeper服务：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4、伪分布式安装"><span class="nav-text">4、伪分布式安装</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5、修改zkServer-cmd"><span class="nav-text">5、修改zkServer.cmd</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6、配置完成-启动zkServer"><span class="nav-text">6、配置完成 启动zkServer</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#7、配置中出现的问题及解决方式："><span class="nav-text">7、配置中出现的问题及解决方式：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三、Zookeeper真分布式集群（未测试）"><span class="nav-text">三、Zookeeper真分布式集群（未测试）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-Dubbo中zookeeper做注册中心-，如果注册中心集群都挂掉-，发布者和订阅者之间还能通信么？"><span class="nav-text">1.Dubbo中zookeeper做注册中心 ，如果注册中心集群都挂掉 ，发布者和订阅者之间还能通信么？</span></a></li></ol></li></ol><li class="nav-item nav-level-3"><a class="nav-link" href="#四、验证服务器"><span class="nav-text">四、验证服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#五、springboot-zookeeperk-dubbo"><span class="nav-text">五、springboot zookeeperk dubbo</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、zk连接测试"><span class="nav-text">1、zk连接测试</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#pom-xml依赖"><span class="nav-text">pom.xml依赖</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#测试集群是否搭建成功："><span class="nav-text">测试集群是否搭建成功：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2、单机测试-分布式-dubbo"><span class="nav-text">2、单机测试-分布式(dubbo)</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#web层-消费者-配置"><span class="nav-text">web层(消费者)配置</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1）-web层-消费者-引入依赖pom-xml"><span class="nav-text">1）.web层(消费者)- 引入依赖pom.xml</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2）-web层-消费者-yml-文件"><span class="nav-text">2）.web层(消费者)- *.yml 文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3）-web层-消费者-spring-dubbo-consumer-xml"><span class="nav-text">3）.web层(消费者)- spring-dubbo-consumer.xml</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#4）-web层-消费者-需要引入api层-公共接口-依赖jar"><span class="nav-text">4）.web层(消费者)- 需要引入api层(公共接口)依赖jar</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#5）-web层-消费者-测试代码："><span class="nav-text">5）.web层(消费者)- 测试代码：</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#service层-提供者-配置"><span class="nav-text">service层(提供者)配置</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#1-service层-提供者-pom-xml"><span class="nav-text">1).service层(提供者)- pom.xml</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#2-service层-提供者-yml文件配置"><span class="nav-text">2).service层(提供者)- *.yml文件配置</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#3-service层-提供者-spring-dubbo-provider-xml"><span class="nav-text">3).service层(提供者)- spring-dubbo-provider.xml</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#4-service层-提供者-需要引入api层-公共接口-依赖jar"><span class="nav-text">4).service层(提供者)- 需要引入api层(公共接口)依赖jar</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#5-service层-提供者-测试代码："><span class="nav-text">5).service层(提供者)- 测试代码：</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#api层-公共接口"><span class="nav-text">api层(公共接口)</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3、伪集群测试-分布式-dubbo"><span class="nav-text">3、伪集群测试-分布式(dubbo)</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#1-web层-消费者-yml文件配置：zookeeper集群配置"><span class="nav-text">1.web层(消费者)- *.yml文件配置：zookeeper集群配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-service层-提供者-yml配置：集群配置"><span class="nav-text">2.service层(提供者)- *.yml配置：集群配置</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#六、Zookeeper安全认证"><span class="nav-text">六、Zookeeper安全认证</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1、为什么Zookeeper要安全认证-？"><span class="nav-text">1、为什么Zookeeper要安全认证 ？</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-ACL认证的简介"><span class="nav-text">2.ACL认证的简介</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-没有ACL认证时zookeeper的操作"><span class="nav-text">3.没有ACL认证时zookeeper的操作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-有ACL认证时zookeeper的操作测试："><span class="nav-text">4.有ACL认证时zookeeper的操作测试：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-zookeeper超级用户配置-Windows-Linux"><span class="nav-text">5.zookeeper超级用户配置(Windows&#x2F;Linux)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#七、Zookeeper-Dubbo认证"><span class="nav-text">七、Zookeeper+Dubbo认证</span></a></li></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="宇宙小神特别萌" src="/my-images/avatar.gif"><p class="site-author-name" itemprop="name">宇宙小神特别萌</p><div class="site-description" itemprop="description">人生就是一次幸运的旅行！</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">116</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">27</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/zhengjiaao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengjiaao" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/70d69269bd09" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;70d69269bd09" rel="noopener" target="_blank"><i class="fa fa-fw fa-book"></i>简书</a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 推荐阅读</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/spring-projects" title="https:&#x2F;&#x2F;github.com&#x2F;spring-projects" rel="noopener" target="_blank">spring-projects</a></li><li class="links-of-blogroll-item"><a href="http://www.alloyteam.com/nav/" title="http:&#x2F;&#x2F;www.alloyteam.com&#x2F;nav&#x2F;" rel="noopener" target="_blank">Web前端导航</a></li><li class="links-of-blogroll-item"><a href="http://www.chuangzaoshi.com/code" title="http:&#x2F;&#x2F;www.chuangzaoshi.com&#x2F;code" rel="noopener" target="_blank">创造狮导航</a></li></ul></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">宇宙小神特别萌</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="站点总字数">1.2m</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">17:41</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-divider">|</span> <span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer></div><script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdnjs.cloudflare.com/ajax/libs/valine/1.3.10/Valine.min.js', () => {
    <!--var GUEST = ['nick', 'mail', 'link'];-->
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: false,
      appId: 'OuyHeA0XGL8hK4vgbqL0iERx-gzGzoHsz',
      appKey: 'L58VglKmWULcGK2W2AN7kWzM',
      placeholder: "留下邮箱，有回复时你将收到提醒，邮箱不会被公开。",
      avatar: 'wavatar',
      meta: guest,
      pageSize: '10' || 10,
      visitor: true,
      lang: 'zh-cn' || 'zh-cn',
      path: location.pathname,
      recordIP: true,
      serverURLs: ''
    });
  }, window.Valine);
});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,model:{jsonPath:"/live2dw/assets/wanko.model.json"},display:{position:"right",width:150,height:300},mobile:{show:!1},log:!1})</script></body><script type="text/javascript" src="/js/src/activetitle.js"></script></html><!-- rebuild by neat -->